//package cn.nfwx.tv.controller;
//
//import android.app.Activity;
//import android.support.v7.widget.RecyclerView;
//import android.text.TextUtils;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.ImageButton;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//
//import com.chad.library.adapter.base.BaseQuickAdapter;
//
//import org.greenrobot.eventbus.EventBus;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.adapter.SearchAdapter;
//import cn.nfwx.tv.base.BaseDialogFragment;
//import cn.nfwx.tv.dao.SubscriptionDao;
//import cn.nfwx.tv.events.PlayEvents;
//import cn.nfwx.tv.fragment.search.SearchFragment;
//import cn.nfwx.tv.helper.ThreadHelper;
//import cn.nfwx.tv.model.ColumnModel;
//import cn.nfwx.tv.model.DescriptionModel;
//import cn.nfwx.tv.model.SubscriptionModel;
//import cn.nfwx.tv.model.TvProgram;
//import cn.nfwx.tv.okhttp.UrlHelper;
//import cn.nfwx.tv.util.GlideUtil;
//import cn.nfwx.tv.util.RecyclerViewUtil;
//import cn.nfwx.tv.widget.ItemHotColumn;
//
///**
// * Created by zhouzechao on 2017/12/11.
// */
//
//public class SearchResultSubController implements View.OnClickListener {
//
//    private Activity mActivity;
//    private BaseDialogFragment mFragment;
//
//    private final LinearLayout ll_subscription, hot_column_content, ll_item_search;
//    private final ImageView program_image;
//    private final TextView program_name, hot_type, hot_descrition;
//    private final ItemHotColumn hot_channel, hot_language, hot_time;
//    private final ImageButton up_btn;
//    private final RecyclerView rcv_vod_search;
//    private final Button subscription_btn, subscription_cancle_btn;
//
//    private String requestSubVodCommand;
//
//    private boolean _isShow = false;
//    private SubscriptionModel mSubscriptionModel;
//    private SearchAdapter mAdapter;
//
////    public SearchResultSubController(SearchActivity searchActivity) {
////        mActivity = searchActivity;
////        ll_subscription = findView(R.id.ll_subscription);
////        ll_item_search = findView(R.id.ll_item_search);
////        hot_column_content = findView(R.id.hot_column_content);
////        program_image = findView(R.id.program_image);
////        program_name = findView(R.id.program_name);
////        hot_type = findView(R.id.hot_type);
////        hot_channel = findView(R.id.hot_channel);
////        hot_language = findView(R.id.hot_language);
////        hot_time = findView(R.id.hot_time);
////        hot_descrition = findView(R.id.hot_descrition);
////        subscription_btn = findView(R.id.subscription_btn);
////        subscription_cancle_btn = findView(R.id.subscription_cancle_btn);
////        up_btn = findView(R.id.up_btn);
////        rcv_vod_search = findView(R.id.rcv_vod_search);
////        findView(R.id.v_spilt).setVisibility(View.GONE);
////
////        initRecyclerView(rcv_vod_search);
////        initListener();
////    }
//
//    public SearchResultSubController(SearchFragment searchFragment) {
//        mActivity = searchFragment.getActivity();
//        mFragment = searchFragment;
//        View view = searchFragment.getView();
//        ll_subscription = findView(view, R.id.ll_subscription);
//        ll_item_search = findView(view, R.id.ll_item_search);
//        hot_column_content = findView(view, R.id.hot_column_content);
//        program_image = findView(view, R.id.program_image);
//        program_name = findView(view, R.id.program_name);
//        hot_type = findView(view, R.id.hot_type);
//        hot_channel = findView(view, R.id.hot_channel);
//        hot_language = findView(view, R.id.hot_language);
//        hot_time = findView(view, R.id.hot_time);
//        hot_descrition = findView(view, R.id.hot_descrition);
//        subscription_btn = findView(view, R.id.subscription_btn);
//        subscription_cancle_btn = findView(view, R.id.subscription_cancle_btn);
//        up_btn = findView(view, R.id.up_btn);
//        rcv_vod_search = findView(view, R.id.rcv_vod_search);
//        findView(view, R.id.v_spilt).setVisibility(View.GONE);
//
//        initRecyclerView(rcv_vod_search);
//        initListener();
//    }
//
//    public void initRecyclerView(RecyclerView recyclerView) {
//        RecyclerViewUtil.setLinearLayoutManagerWithoutDivder(recyclerView.getContext(), recyclerView);
//        mAdapter = new SearchAdapter();
//        recyclerView.setAdapter(mAdapter);
//    }
//
//    private void initListener() {
//        up_btn.setOnClickListener(this);
//        ll_item_search.setOnClickListener(this);
//        hot_column_content.setOnClickListener(this);
//        subscription_btn.setOnClickListener(this);
//        subscription_cancle_btn.setOnClickListener(this);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
//                if (adapter.getData().get(position) instanceof ColumnModel) { // 播放对应的视频
//                    ColumnModel columnModel = (ColumnModel) adapter.getData().get(position);
//                    //VodPlayerActivity.startActivity(mActivity, columnModel.getPlaypath(), null, columnModel.getName(), 0);
//                    EventBus.getDefault().post(new PlayEvents(columnModel.getPlaypath(), null, columnModel.getName(), 0));
//                } else if (adapter.getData().get(position) instanceof TvProgram) {
//                    ((SearchFragment) mFragment).isBackEvent = false;
//                    EditText et_keyword = findView(mFragment.getView(), R.id.et_keyword);
//                    String keyword = et_keyword.getEditableText().toString();
//                    TvProgram tvProgram = (TvProgram) adapter.getData().get(position);
//                    PlayEvents events = new PlayEvents(tvProgram.getProgram_id(), tvProgram.getUrl(), null, tvProgram.getProgram_name(), 0);
//                    events.setFragment(mFragment);
//                    events.setKeyword(keyword);
//                    events.setTag("search");
//                    EventBus.getDefault().post(events);
//                    mFragment.dismissAllowingStateLoss();
//                }
//            }
//        });
//
//        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
//                if (adapter.getData().get(position) instanceof ColumnModel) { // 播放对应的视频
//                    ColumnModel columnModel = (ColumnModel) adapter.getData().get(position);
//                    switch (view.getId()) {
//                        case R.id.btn_appiontment_downlaod: // 下载
////                            if (AccountHelper.getInstance().isLogin(mActivity)) {
////                                if (TextUtils.isEmpty(DownLoadManager.getFileVsid(columnModel.getPlaypath()))) {
////                                    DialogUtil.showLoadingProgress(mActivity);
////                                    DownLoadManager.download(mActivity, columnModel.getPlaypath(), new Interfaces.OnFileDownloadCreatedListener() {
////                                        @Override
////                                        public void onCreated(String vsid) {
////                                            Toast.makeText(mActivity, "已添加到下载队列", Toast.LENGTH_LONG).show();
////                                            DialogUtil.cancleLoadingProgress();
////
////                                            // 下载ui更新
////                                            downNotifyData(position);
////                                        }
////                                    }, new Interfaces.OnFileDownloadFailedListener() {
////                                        @Override
////                                        public void onFailed(String vsid) {
////                                            DialogUtil.cancleLoadingProgress();
////                                            Toast.makeText(mActivity, "添加下载失败", Toast.LENGTH_LONG).show();
////                                        }
////                                    });
////                                } else {
////                                    Toast.makeText(mActivity, "已添加到下载队列", Toast.LENGTH_LONG).show();
////                                    downNotifyData(position);
////                                }
////                            } else {
////                                Toast.makeText(mActivity, R.string.download_no_login, Toast.LENGTH_LONG).show();
////                            }
//                            break;
//                    }
//                }
//            }
//        });
//    }
//
//    /**
//     * 下载更新状态
//     *
//     * @param position
//     */
//    public void downNotifyData(int position) {
//        if (mAdapter.getData().get(position) instanceof ColumnModel) {
//            ColumnModel columnModel = (ColumnModel) mAdapter.getData().get(position);
//            columnModel.setStatus(ColumnModel.PROGRAM_STATUS_DOWNING);
//            mAdapter.notifyItemChanged(position);
//        }
//    }
//
//    /**
//     * 显示控件
//     *
//     * @param subscriptionModel
//     */
//    public void show(SubscriptionModel subscriptionModel) {
//        _isShow = true;
//        ll_subscription.setVisibility(View.VISIBLE);
//        mSubscriptionModel = subscriptionModel;
//        subscriptionModel.loadData();
//        if (!TextUtils.isEmpty(subscriptionModel.getAssets1())) {
//            GlideUtil.loadCircleImage(mActivity, UrlHelper.getSubscriptionThumbUrl(subscriptionModel.getAssets1()), program_image);
//        } else {
//            GlideUtil.loadCircleImage(mActivity, null, program_image);
//        }
//        program_name.setText(subscriptionModel.getName());
//        hot_column_content.setVisibility(View.GONE);
//        if (subscriptionModel.getDescriptionModel() != null) {
//            DescriptionModel de = subscriptionModel.getDescriptionModel();
//            hot_type.setText(de.getType());
//            hot_descrition.setText(de.getDescription());
//            hot_channel.setText(de.getChannel());
//            hot_language.setText(de.getLanguage());
//            hot_time.setText(de.getShowTime());
//        } else {
//            hot_type.setText("");
//            hot_descrition.setText("");
//            hot_channel.setText("");
//            hot_language.setText("");
//            hot_time.setText("");
//        }
//        requestSubVods(subscriptionModel.getId());
//
//        SubscriptionDao dao = new SubscriptionDao(mActivity);
//        if (dao.query(subscriptionModel.getId()).size() == 0) {
//            subscription_btn.setVisibility(View.VISIBLE);
//            subscription_cancle_btn.setVisibility(View.GONE);
//        } else {
//            subscription_btn.setVisibility(View.GONE);
//            subscription_cancle_btn.setVisibility(View.VISIBLE);
//        }
//    }
//
//    /**
//     * 隐藏控件
//     */
//    public void hidden() {
//        _isShow = false;
//        ll_subscription.setVisibility(View.GONE);
//    }
//
//    public boolean isShow() {
//        return _isShow;
//    }
//
//    private void requestSubVods(String cid) {
////        requestSubVodCommand = SubscriptionManager.requestVideoList(mActivity, cid, new ResponseListener<JSONObject>() {
////            @Override
////            public void onResponse(final String requestTag, final JSONObject response) {
////                ThreadHelper.post(new Runnable() {
////                    @Override
////                    public void run() {
////                        Gson gson = new Gson();
////                        if (requestTag.equals(requestSubVodCommand)) {
////                            try {
////                                final List<ColumnModel> columns = gson.fromJson(response.getString("result"), new TypeToken<List<ColumnModel>>() {
////                                }.getType());
////                                List<P2PSMgr.P2PSFileInfo> fileInfos = DownLoadManager.getDownLoadList(mActivity);
////                                for (ColumnModel columnModel : columns) {
////                                    columnModel.setStatus(ColumnModel.PROGRAM_STATUS_LOOKBACK);
////                                    for (P2PSMgr.P2PSFileInfo fileInfo : fileInfos) {
////                                        if (fileInfo.vid.contains(columnModel.getId())) {
////                                            columnModel.setVsid(fileInfo.vsid);
////                                            if (fileInfo.percent == 100) {
////                                                columnModel.setStatus(ColumnModel.PROGRAM_STATUS_DOWNED);
////                                            } else {
////                                                columnModel.setStatus(ColumnModel.PROGRAM_STATUS_DOWNING);
////                                            }
////                                            break;
////                                        }
////                                    }
////                                }
////                                ArrayList<Object> objects = new ArrayList<Object>();
////                                objects.addAll(columns);
////                                mAdapter.setNewData(objects);
////                            } catch (JSONException e) {
////                                e.printStackTrace();
////                            }
////                        }
////                    }
////                });
////            }
////
////            @Override
////            public void onError(String requestTag, Throwable t) {
////
////            }
////
////            @Override
////            public void onFinish() {
////
////            }
////        });
//    }
//
//    /**
//     * 查找控件
//     *
//     * @param rid
//     * @param <T>
//     * @return
//     */
//    public <T extends View> T findView(int rid) {
//        return (T) mActivity.findViewById(rid);
//    }
//
//    public <T extends View> T findView(View view, int rid) {
//        return (T) view.findViewById(rid);
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.up_btn:
//            case R.id.hot_column_content:
//                hot_column_content.setVisibility(View.GONE);
//                break;
//            case R.id.ll_item_search:
//                if (hot_column_content.getVisibility() == View.GONE) {
//                    hot_column_content.setVisibility(View.VISIBLE);
//                } else {
//                    hot_column_content.setVisibility(View.GONE);
//                }
//                break;
//            case R.id.subscription_btn:
//                if (mSubscriptionModel == null) {
//                    return;
//                }
//                AccountController.subscription(mActivity, mSubscriptionModel);
//                ThreadHelper.removeRunnable(resetRunable);
//                ThreadHelper.postDelayed(resetRunable, 500);
//                break;
//            case R.id.subscription_cancle_btn:
//                if (mSubscriptionModel == null) {
//                    return;
//                }
//                //AccountController.cancelsubscription(mActivity, mSubscriptionModel);
//                ThreadHelper.removeRunnable(resetRunable);
//                ThreadHelper.postDelayed(resetRunable, 500);
//                break;
//        }
//    }
//
//    Runnable resetRunable = new Runnable() {
//        @Override
//        public void run() {
//            SubscriptionDao dao = new SubscriptionDao(mActivity);
//            if (dao.query(mSubscriptionModel.getId()).size() == 0) {
//                subscription_btn.setVisibility(View.VISIBLE);
//                subscription_cancle_btn.setVisibility(View.GONE);
//            } else {
//                subscription_btn.setVisibility(View.GONE);
//                subscription_cancle_btn.setVisibility(View.VISIBLE);
//            }
//        }
//    };
//}
